const { Field } = require('../../assets/libs/zanui/index');

var app = getApp();
Page(Object.assign({}, Field, {

  data: {
    base: {
      account: {
        name: 'account',
        focus: true,
        title: '账号',
        placeholder: '用户名'
      },
      password: {
        name: 'password',
        title: '密码',
        inputType: 'password',
        placeholder: '请输入你的密码'
      },
    }
  },

  onLoad: function (options) {
 
  },

  onShow: function () {
    
    if (!wx.getStorageSync('userInfo')) {
      app.error("请登录后再操作", function () {
        setTimeout(function () { wx.navigateBack({}) }, 2000);
      });
    }

   
  },

  formSubmit(event) {

    var that = this;
    event.detail.value.openid=wx.getStorageSync('openid')
    if(!event.detail.value.account){
      wx.showModal({
        title: '温馨提示',
        content: '请输入正确的账号',
        showCancel: false
      });
      return false
    }

    if(!event.detail.value.password){
      wx.showModal({
        title: '温馨提示',
        content: '请输入正确的密码',
        showCancel: false
      });
      return false
    }

    wx.request({
      url: app.apiUrl + '/api/index/bangding',
      data: {
        account: event.detail.value.account,
        password: event.detail.value.password,
        openid: wx.getStorageSync('openid'),
        image:wx.getStorageSync('userInfo').avatarUrl,
        wxnickname:wx.getStorageSync('userInfo').nickName
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res=> {
         console.log(res.data)
          if(res.data == 1){
            wx.showModal({
              title: '温馨提示',
              content: '绑定成功',
              showCancel: false
            });


            setTimeout(function () { 
            
              wx.navigateTo({
                url: '/page/my/index'
              });
            }, 5000);
          
          }else{
            wx.showModal({
              title: '温馨提示',
              content: '绑定失败',
              showCancel: false
            });
          }
      }
    })
  
  },

  formReset(event) {

  },
}))